package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.QueryWrapperUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import com.sk.bilibili.bean.Type;
import com.sk.bilibili.mapper.TypeMapper;

/**
 * @author 邵康
 * @date 2021/11/23 18:45
 */
@Service
public class TypeService extends ServiceImpl<TypeMapper, Type> {

    @Autowired
    private MinioService minioService;
    private static final String COVER_PATH = "type/cover/%s.jpg";
    private static final Pattern COVER_PATTERN = Pattern.compile("/(type/cover/.*jpg)");
    private static final String SMALLCOVER_PATH = "type/smallCover/%s.jpg";
    private static final Pattern SMALLCOVER_PATTERN = Pattern.compile("/(type/smallCover/.*jpg)");

    public R<Void> add(Type type) {
        type.setCover(minioService.add(type.getCoverFile(), COVER_PATH));
        type.setSmallCover(minioService.add(type.getSmallCoverFile(), SMALLCOVER_PATH));
        save(type);
        return R.success();
    }

    public R<Void> delete(Integer id) {
        Type temp = getById(id);
        minioService.delete(temp.getCover(), COVER_PATTERN);
        minioService.delete(temp.getSmallCover(), SMALLCOVER_PATTERN);
        removeById(id);
        return R.success();
    }

    public R<Void> update(Type type) {
        Type temp = getById(type.getId());
        String cover = minioService.update(COVER_PATH, COVER_PATTERN, temp.getCover(), type.getCoverFile());
        type.setCover(cover);
        String smallCover = minioService.update(SMALLCOVER_PATH, SMALLCOVER_PATTERN,
                temp.getSmallCover(), type.getSmallCoverFile());
        type.setSmallCover(smallCover);
        updateById(type);
        return R.success();
    }

    public R<IPage<Type>> page(Integer page, Integer size) {
        return R.success(page(new Page<>(page, size), null));
    }

    public R<List<Type>> getAll() {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("priority");
        return R.success(list(queryWrapper));
    }

    public boolean isThem(Integer videoSetId) {
        return baseMapper.isThem(videoSetId);
    }

    public List<Type> getTypeList(List<Map<String, Long>> list) {
        List<Type> res = new ArrayList<>();
        long sum = 0;
        for (Map<String, Long> map : list) {
            Type type = getById(map.get("type_id"));
            Long num = map.get("num");
            type.getExtra().put("num", num);
            res.add(type);
            sum += num;
        }
        Type type = new Type();
        type.setTitle("全部");
        type.getExtra().put("num", sum);
        res.add(0, type);//全部放在第一位
        return res;
    }

}
